package com.android.questions.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.android.base.BaseLibrary;
import com.android.base.util.AppFrontBackHelper;
import com.android.base.util.DQLog;
import com.android.base.util.HomeReceiver;
import com.android.base.util.Mmkv;
import com.android.base.util.SystemUtil;
import com.android.questions.R;
import com.android.questions.config.AppConfig;
import com.android.questions.config.LoginConfig;
import com.android.questions.ui.activity.LoginActivity;
import com.android.questions.ui.activity.SplashActivity;
import com.android.questions.util.sound.MediaBackManager;
import com.dq.ad.AdManager;
import com.qq.e.o.HXADConfig;

import org.litepal.LitePal;

import java.util.Random;

/**
 * FileName: QuestionsApp
 * Author: admin
 * Date: 2021/11/1 16:26
 * Description:
 */
public class QuestionsApp extends Application {
    public static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

        BaseLibrary.init(this);
        LitePal.initialize(this);

        LoginConfig.APP_ID_WX = getString(R.string.WX_APPID);
        AppConfig.UMENG_KEY = getString(R.string.UMENG_KEY);
        AppConfig.HX_APP_ID = getString(R.string.HX_APPID);
        AppConfig.CH_ID = getString(R.string.CH_ID);
        AppConfig.CP_ID = getString(R.string.CP_ID);
        AppConfig.APP_CODE = getString(R.string.APP_CODE_RELEASE);
//        AppConfig.TT_APP_ID = getString(R.string.TT_APPID);
        AppConfig.QQ_GROUP = "640089641";
        AppConfig.QQ_GROUP_KEY = "sufFiFgflyEhgPiilXT_D4BGhFU6Ri0E";
        AdManager.channel = AppConfig.CH_ID;

        if (SystemUtil.isToday()) {//今天第二次第N次打开
            DQLog.e("今天第N次打开");
            if (SystemUtil.is8()) {//每天第N次打开，并且在8点后
                if (!AppConfig.getResetOnlineState()) {//如已经重置过，不再进行重置
                    reset();
                }
            } else {
                AppConfig.setResetOnlineState(false);
            }
        } else {//今天第一次打开
            SystemUtil.setToday();
            DQLog.e("今天第一次打开");
            AppConfig.setDayOnLineTime(0);      //当天在线时长
            int loginDay = AppConfig.getLoginDayNumber() + 1;
            AppConfig.setLoginDayNumber(loginDay);  //累计登陆天数

            if (SystemUtil.is8()) {//每天第一次打开，并且在8点后
                reset();
            } else {
                AppConfig.setResetOnlineState(false);
            }
        }

        if (!Mmkv.contains("total_piggy")) {
            AppConfig.setTotalPiggy(1000000);
        }
        if (!Mmkv.contains("cash_cow")) {
            AppConfig.setCashCow(195.6);
        }
        if (!Mmkv.contains("remain_withdrawal_times")) {
            AppConfig.setRemainWithdrawalTimes(10);
        }

        if (!Mmkv.contains("ad_recommend")) {
//            DQLog.e("set ad recommend state");
            Mmkv.setBoolean("ad_recommend", true);
        }
        HXADConfig.setPersonalizedSwitch(Mmkv.getBoolean("ad_recommend"));

        AppFrontBackHelper.getInstance().register(this, new AppFrontBackHelper.OnAppStatusListener() {
            @Override
            public void onFront(Activity activity) {
                //应用切到前台处理
                if (!(activity instanceof SplashActivity) && !(activity instanceof LoginActivity)) {
                    if (HomeReceiver.isBackground) {
                        HomeReceiver.isBackground = false;
//                        AdManager.getInstance().showInterstitialAD(activity);
                        Intent intent = new Intent(activity, SplashActivity.class);
                        intent.putExtra("key_from", "app");
                        activity.startActivity(intent);
                    }
                }
            }

            @Override
            public void onBack(Activity activity) {
                if (AppConfig.getBackMusic()) {
                    MediaBackManager.getInstance(getApplicationContext()).pause();
                }
                if (HomeReceiver.isHome(getApplicationContext())) {
                    HomeReceiver.isBackground = true;
                }
            }
        });
    }

    private void reset() {
        AppConfig.setDayAnswerNumber(0);
        AppConfig.setResetOnlineState(true);
        AppConfig.setExtractTimes(0);
//        AppConfig.setShowDaily1(false);
//        AppConfig.setShowDaily2(false);
        int nextInt = new Random().nextInt(400) + 800;
        AppConfig.setTodayPeople(nextInt);
        AppConfig.setRemainWithdrawalTimes(10);
    }
}